package cn.edu.fzu.homemaking.wallet.controller;

import cn.edu.fzu.homemaking.common.PageVO;

import cn.edu.fzu.homemaking.common.SessionConstant;
import cn.edu.fzu.homemaking.wallet.api.SsoApi;
import cn.edu.fzu.homemaking.wallet.domain.dto.User;
import cn.edu.fzu.homemaking.wallet.domain.entity.MoneyWater;
import cn.edu.fzu.homemaking.wallet.domain.enums.ChargeModeEnum;
import cn.edu.fzu.homemaking.wallet.service.WalletService;
import cn.edu.fzu.homemaking.web.Result;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.Objects;

@Api(tags = "WALLET-钱包模块")
@RestController
@RequestMapping("/api/wallet/")
public class WalletController {

    @Resource
    private WalletService walletService;

    @Resource
    private SsoApi        ssoApi;

    @ApiOperation(value = "获取个人钱包接口")
    @GetMapping("/getWallet")
    public Result get(HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute(SessionConstant.ACCOUNT);
        return new Result(walletService.getByUserId(user.getId()));
    }


    @ApiOperation(value = "初始化流水表接口")
    @GetMapping("/initWater")
    public Result initWater(HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute(SessionConstant.ACCOUNT);
        walletService.initWater(user.getId());
        return Result.SUCCESS;
    }


    @ApiOperation(value = "删除流水表接口")
    @GetMapping("/dropWater")
    public Result dropWater(HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute(SessionConstant.ACCOUNT);
        walletService.dropTemplate(user.getId());
        return Result.SUCCESS;
    }


    @ApiOperation(value = "充值", httpMethod = "GET")
    @ApiImplicitParams({ @ApiImplicitParam(name = "money", required = true, value = "充值金额") })
    @GetMapping("/recharge")
    public Result recharge(BigDecimal money, HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute(SessionConstant.ACCOUNT);
        walletService.recharge(user, money);
        ssoApi.setSession(SessionConstant.RECHARGE, money);
        return Result.SUCCESS;
    }


    @ApiOperation(value = "转账")
    @ApiImplicitParams({ @ApiImplicitParam(name = "description", required = true, value = "备注"),
            @ApiImplicitParam(name = "to", required = true, value = "被转账人"),
            @ApiImplicitParam(name = "money", required = true, value = "转账金额") })
    @PostMapping("/transfer")
    public Result transfer(Long to, BigDecimal money, String description, HttpServletRequest request) {
        HttpSession session = request.getSession();
        User user = (User) request.getSession().getAttribute(SessionConstant.ACCOUNT);
        String userType = (String) session.getAttribute(SessionConstant.USER_TYPE);
        walletService.transfer(user, userType, to, money, description);
        return Result.SUCCESS;
    }


    @ApiOperation(value = "查看转账流水")
    @ApiImplicitParams({ @ApiImplicitParam(name = "pageNum", required = true),
            @ApiImplicitParam(name = "pageSize", required = true) })
    @GetMapping("/getWater")
    public Result getWater(ChargeModeEnum chargeMode, Integer pageNum, Integer pageSize, HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute(SessionConstant.ACCOUNT);
        try (Page<MoneyWater> page = PageHelper.startPage(pageNum, pageSize)) {
            walletService.listWaterTemplate(user.getId(), chargeMode);
            return new Result(new PageVO<>(page.getTotal(), page.getResult()));
        }
    }


    @ApiOperation(value = "查看转账流水详情")
    @GetMapping("/getWaterDetail")
    public Result getWaterDetail(Long id, HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute(SessionConstant.ACCOUNT);
        MoneyWater water = walletService.getWaterDetail(user.getId(), id);
        return new Result(water);
    }
}
